var $orderSelectedProjectsCount = $('#order-selected-projects-count');
var $orderProgress = document.querySelectorAll('[data-order-progress-responsive]');
var $projectItem = $('#project-<%= project.id %>-item');
var $projectBudgetButton = $('#project-<%= project.id %>-budget-button');
var $budgetConfirm = $('#budget-confirm');

$orderProgress.forEach((orderProgress) => {
  var $orderTotalBudget = $(orderProgress).find("[id^=order-total-budget]");
  $orderTotalBudget.html('<%= j(render partial: "decidim/budgets/projects/order_total_budget").strip.html_safe %>');

  if(orderProgress.dataset.orderProgressResponsive === "true") {
    morphdom(orderProgress, '<%= j(render partial: "decidim/budgets/projects/order_progress_summary/content_responsive", locals: { focus_mode_origin: @focus_mode_origin }).strip.html_safe %>');
  } else {
    morphdom(orderProgress, '<%= j(render partial: "decidim/budgets/projects/order_progress_summary/content").strip.html_safe %>');
  }
});

morphdom($budgetConfirm[0], '<%= j(render partial: "decidim/budgets/projects/budget_confirm").strip.html_safe %>')
if ($orderSelectedProjectsCount.length > 0) {
  morphdom($orderSelectedProjectsCount[0], '<%= j(render partial: "decidim/budgets/projects/projects_count", locals: { count: current_order.projects.size }).strip.html_safe %>')
}

if ($projectItem.length > 0) {
  <% if params[:action] == "destroy" && params[:show_only_added] == "true" %>
    $projectItem[0].remove();
  <% else %>
    morphdom($projectItem[0], '<%= j(render partial: "decidim/budgets/projects/project", locals: { project: }).strip.html_safe %>');
  <% end %>
}

if ($projectBudgetButton.length > 0) {
  morphdom($projectBudgetButton[0], '<%= j(render partial: "decidim/budgets/projects/project_budget_button", locals: { project: }).strip.html_safe %>');
}

window.DecidimBudgets.checkProgressPosition();
